﻿using System;
using System.Web;

namespace Core.Helper
{
    public sealed class Cookie
    {
        public static void Add(string key, object value)
        {
            HttpCookie cookie = new HttpCookie(key);
            cookie.Value = value.ToString();
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        public static string Get(string key)
        {
            if (!HasKey(key)) { return null; }
            return HttpContext.Current.Request.Cookies[key].Value;
        }

        public static void Remove(string key)
        {
            if (HasKey(key))
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
                cookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
        }

        public static bool HasKey(string key)
        {
            return HttpContext.Current.Request.Cookies[key] != null;
        }
    }
}